package com.dsdl.chatapp.common.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.dsdl.chatapp.BaseApplication;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by zzg on 2016/8/1.
 * 数据库管理类
 */
public class DBManager implements IDBManager{
    private static volatile DBManager mDbManager;
    private SQLiteDatabase mDataBase;
    private Lock mLock = new ReentrantLock();

    private DBManager(Context context){
        DBHelper dbHelper = new DBHelper(context);
        mDataBase = dbHelper.getWritableDatabase();
    }

    /**
     * 获取单例
     * @return DBManager对象
     */
    public static synchronized DBManager getInstance() {
        mDbManager = new DBManager(BaseApplication.getInstance());
        return mDbManager;
    }
    @Override
    public SQLiteDatabase getDatabase() {
        return mDataBase;
    }

    @Override
    public long insert(String table, ContentValues values) {
        try{
            mDbManager.lock();
            openDB();
            if (mDataBase != null){
                return mDataBase.insert(table,null,values);
            }
        }finally {
            closeDB();
            mDbManager.unLock();
        }
        return 0;
    }

    @Override
    public int delete(String table, String whereClause, String[] whereArgs) {
        try{
            mDbManager.lock();
            openDB();
            if (mDataBase != null){
                return mDataBase.delete(table,whereClause,whereArgs);
            }
        }finally {
            closeDB();
            mDbManager.unLock();
        }
        return 0;
    }

    @Override
    public long update(String table, ContentValues values, String whereClause, String[] whereArgs) {
        try{
            mDbManager.lock();
            openDB();
            if (mDataBase != null){
                return mDataBase.update(table,values,whereClause,whereArgs);
            }
        }finally {
            closeDB();
            mDbManager.unLock();
        }
        return 0;
    }

    @Override
    public long insertOrUpdate(String table, ContentValues values, String whereClause, String[] whereArgs) {
        Cursor cursor = null;
        try{
            mDbManager.lock();
            openDB();
            cursor = mDataBase.query(table,null,whereClause,whereArgs,null,null,null,null);
            if (cursor.getCount() > 0){
                if (mDataBase != null){
                    return mDataBase.update(table,values,whereClause,whereArgs);
                }
            }else {
                if (mDataBase != null){
                    return mDataBase.insert(table,null,values);
                }
            }

        }finally {
            if (cursor != null){
                cursor.close();
                cursor = null;
            }
            closeDB();
            mDbManager.unLock();
        }
        return 0;
    }

    @Override
    public Cursor rawQuery(String sql, String[] selectionArgs) {
        try{
            mDbManager.lock();
            openDB();
            if (mDataBase != null){
                return mDataBase.rawQuery(sql,selectionArgs);
            }
        }finally {
            closeDB();
            mDbManager.unLock();
        }
        return null;
    }

    @Override
    public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {
        try{
            mDbManager.lock();
            openDB();
            if (mDataBase != null){
                return mDataBase.query(table,columns,selection,selectionArgs,groupBy,having,orderBy,limit);
            }
        }finally {
            closeDB();
            mDbManager.unLock();
        }
        return null;
    }

    /**
     * 锁数据库
     */
    private void lock() {
        if (mLock == null){
            mLock = new ReentrantLock();
        }
        mLock.lock();
    }

    /**
     * 解锁数据库
     */
    private void unLock() {
        if (mLock == null){
            return;
        }
        mLock.unlock();
    }

    /**
     * 打开数据库
     */
    private void openDB() {
        if (mDataBase != null && mDataBase.isOpen()){
            return;
        }
        DBHelper dbHelper = new DBHelper(BaseApplication.getInstance());
        mDataBase = dbHelper.getWritableDatabase();
    }

    /**
     * 关闭数据库
     */
    private void closeDB() {
        if (mDataBase != null) {
            mDataBase.close();
            mDataBase = null;
        }
    }
}
